﻿using ERP.Framework.Components.LoggingHelper.ErrorLog;
using ERP.Framework.Components.Utilities.TypeHelper;

namespace ERP.Framework.Components.DatabaseHelper.Providers
{
    public sealed class GeneralDatabaseProvider : IDataBaseConnector
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralDatabaseProvider"/> class.
        /// </summary>
        /// <param name="databaseName">Name of the database.</param>
        public GeneralDatabaseProvider(string databaseName)
        {
            string m_providerName = ConfigurationHelper.ConfigurationManager.ConnectionSettings.Databases[databaseName].Provider;
            m_CurrentProvider = TypeFactory.CreateInstance(m_providerName, new object[]{
            ConfigurationHelper.ConfigurationManager.ConnectionSettings.Databases[databaseName].ConnectionString}) as IDataBaseConnector;
            if (m_CurrentProvider == null)
                throw new Exceptions.DatabaseProviderException();
        }

        #endregion

        #region Members

        private IDataBaseConnector m_CurrentProvider = null;
        private string m_DatabseName = string.Empty;

        #endregion

        #region Properties

        /// <summary>
        /// the database connection string
        /// </summary>
        /// <value></value>
        public string ConnectionString
        {
            get
            {
                return m_CurrentProvider.ConnectionString;
            }
            set
            {
                m_CurrentProvider.ConnectionString = value;
            }
        }

        /// <summary>
        /// the exceptions publishing method
        /// </summary>
        /// <value></value>
        public ErrorLogType ErrorLogType
        {
            get
            {
                return m_CurrentProvider.ErrorLogType;
            }
            set
            {
                m_CurrentProvider.ErrorLogType = value;
            }
        }

        /// <summary>
        /// the exceptions log file path
        /// in case the log type was XML file
        /// </summary>
        /// <value></value>
        public string ErrorLogFilePath
        {
            get
            {
                return m_CurrentProvider.ErrorLogFilePath;
            }
            set
            {
                m_CurrentProvider.ErrorLogFilePath = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// excecute update or insert statment
        /// </summary>
        /// <param name="type">stored proc/ text</param>
        /// <param name="command">command text</param>
        /// <param name="parameters">params collection</param>
        /// <returns>count of effected rows</returns>
        public int Save(CommandType type, string command, ParametersCollection parameters)
        {
            return m_CurrentProvider.Save(type, command, parameters);
        }

        /// <summary>
        /// executes a select statement
        /// </summary>
        /// <param name="type">stored proc/ text</param>
        /// <param name="command">command text</param>
        /// <param name="parameters">params collection</param>
        /// <returns>table contains the query result</returns>
        public System.Data.DataTable Read(CommandType type, string command, ParametersCollection parameters)
        {
            return m_CurrentProvider.Read(type, command, parameters);
        }

        /// <summary>
        /// executes a select statement that returns one value [count, max ,...]
        /// </summary>
        /// <param name="type">stored proc/ text</param>
        /// <param name="command">command text</param>
        /// <param name="parameters">params collection</param>
        /// <returns>object contains the query result</returns>
        public object ReadScalar(CommandType type, string command, ParametersCollection parameters)
        {
            return m_CurrentProvider.ReadScalar(type, command, parameters);
        }

        #endregion
    }
}